

package GUI;

/**
 * @author David Ponce
 * @author Philipp Armbruster
 */


import Interaction.ServerConnection;
import Flightadministration.Flightadministration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class PassengerEdit extends JPanel {

    final Main td;
    private JPanel jp;
    public JLabel label2;
    public JTextField textfield1;
    public JTextField textfield2;
    public JTextField textfield3;
    public JTextField textfield4;
    public JTextField textfield5;
    /*Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
    *Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();


    // Konstruktor
    public PassengerEdit(final Main td) throws RemoteException {
    
        this.td = td;
        showPassengerEdit();
    }


    // Methoden
    public void showPassengerEdit() throws RemoteException{

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************
        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 3;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren


        //Label
        //**********************************************************************
        
        JLabel headline = new JLabel("<html><h2 color=blue>Passagier bearbeiten</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        label2 = new JLabel();

        JLabel label3 = new JLabel("Nachname: ");
        constraints.gridx = 0;
        constraints.gridy = 1;
        jp.add(label3, constraints);

        textfield1 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 1;
        jp.add(textfield1, constraints);

        JLabel label4 = new JLabel("Vorname: ");
        constraints.gridx = 0;
        constraints.gridy = 2;
        jp.add(label4, constraints);

        textfield2 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 2;
        jp.add(textfield2, constraints);

        JLabel label5 = new JLabel("Straße: ");
        constraints.gridx = 0;
        constraints.gridy = 3;
        jp.add(label5, constraints);

        textfield3 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 3;
        jp.add(textfield3, constraints);

        JLabel label6 = new JLabel("Ort: ");
        constraints.gridx = 0;
        constraints.gridy = 5;
        jp.add(label6, constraints);

        textfield4 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 5;
        jp.add(textfield4, constraints);

        JLabel label7 = new JLabel("PLZ: ");
        constraints.gridx = 0;
        constraints.gridy = 4;
        jp.add(label7, constraints);

        textfield5 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 4;
        jp.add(textfield5, constraints);


        //Buttons
        //**********************************************************************

        JButton button_UpdatePassenger = new JButton("Änderungen speichern");
        constraints.gridx = 0;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        jp.add(button_UpdatePassenger, constraints);

        JButton button_Cancel = new JButton("Zurück zur Übersicht");
        constraints.gridx = 1;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        jp.add(button_Cancel, constraints);


        //Action Listener
        //**********************************************************************

        button_UpdatePassenger.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                if ((textfield1.getText().equals("")) ||
                    (textfield2.getText().equals("")) ||
                    (textfield3.getText().equals("")) ||
                    (textfield4.getText().equals("")) ||
                    (textfield5.getText().equals("")))
                {
                    JOptionPane.showMessageDialog(null, "Bitte alle Felder ausfüllen!");
                }
                else {
                    try {
                        String currentPLZ = textfield5.getText();

                        if(currentPLZ.length() != 5) {
                            JOptionPane.showMessageDialog(null, "<html><p style=\"color:red; text-align: center\">" +
                                                                "Die Postleitzahl muss immer 5-stellig sein,<br>" +
                                                                "bitte korregieren Sie ihre Eingabe!</p></html>");
                        }
                        else {
                            int currentPLZAsInt = Integer.parseInt(currentPLZ);

                            String id = label2.getText();
                            int idAsInt = Integer.parseInt(id);

                            admin.updatePassenger(idAsInt, textfield2.getText(), textfield1.getText(), textfield3.getText(),
                                    currentPLZAsInt, textfield4.getText());

                            // JTables aktualisieren
                            td.updatePassenger();

                            JOptionPane.showMessageDialog(null, "Fluggast geändert");

                            ((CardLayout) td.rightp.getLayout()).show(td.rightp, "PassengerM");
                        }

                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(null, "<html><p style=\"color:red; text-align: center\">" +
                                                            "Die Postleitzahl muss ausschließlich aus Zahlen bestehen!" +
                                                            "</p></html>");
                    } catch (RemoteException ex) {
                        Logger.getLogger(AircraftCreate.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });


        button_Cancel.addActionListener(new ActionListener() {
                @Override
             public void actionPerformed(ActionEvent e){

                ((CardLayout) td.rightp.getLayout()).show(td.rightp,"PassengerM");
            }
        });

    }

    public JPanel getJp() {
    return jp;
    }
}
